import 'package:get/get_state_manager/get_state_manager.dart';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter_jd/pages/goods/detail/view.dart';
import 'package:get/route_manager.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

// 商品列表
class GoodsList extends GetWidget {
  final List goodsList;
  GoodsList({required this.goodsList});

  @override
  Widget build(BuildContext context) {
    return Obx(
      () => Container(
        margin: EdgeInsets.only(top: 10.h, left: 10.w, right: 10.w),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(10.r),
        ),
        child: GridView(
          shrinkWrap: true,
          padding: EdgeInsets.all(0),
          gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 2,
            crossAxisSpacing: 6.w,
            mainAxisSpacing: 6.w,
            childAspectRatio: 7 / 8.9,
          ),
          physics: NeverScrollableScrollPhysics(),
          children: _buildProductList(),
        ),
      ),
    );
  }

  _buildProductList() {
    List<Widget> list = [];

    for (var i = 0; i < goodsList.length; i++) {
      list.add(
        GestureDetector(
          onTap: () {
            Get.to(
              () => GoodsDetail(arguments: {
                'imgUrl': goodsList[i]['src'],
                'name': goodsList[i]['name'],
                'price': goodsList[i]['price'],
              }),
            );
          },
          child: Container(
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(5.r),
            ),
            child: Column(
              children: [
                AspectRatio(
                  aspectRatio: 1 / 0.93,
                  child: Container(child: Image.asset(goodsList[i]['src'])),
                ),
                Container(
                  alignment: Alignment.topLeft,
                  padding: EdgeInsets.symmetric(horizontal: 8.w),
                  child: Text(
                    goodsList[i]['name'],
                    maxLines: 2,
                    overflow: TextOverflow.ellipsis,
                    style: TextStyle(
                      color: Color(0xFF333333),
                      fontSize: 13.sp,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
                Container(
                  padding: EdgeInsets.only(left: 8.w, top: 3.h),
                  child: Row(
                    children: [
                      Text(
                        '¥',
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      Text(
                        goodsList[i]['price'],
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 14.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                      Text(
                        '.00',
                        style: TextStyle(
                          color: Colors.red,
                          fontSize: 10.sp,
                          fontWeight: FontWeight.w500,
                        ),
                      )
                    ],
                  ),
                )
              ],
            ),
          ),
        ),
      );
    }

    return list;
  }
}
